package com.example.gzysmartcity.ui.fragment.hosp;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.ToastUtils;
import com.example.gzysmartcity.R;
import com.example.gzysmartcity.databinding.FragmentHospBinding;
import com.example.gzysmartcity.repository.model.HospData;
import com.example.gzysmartcity.ui.adapter.HospRvAdapter;
import com.example.gzysmartcity.ui.base.BaseFragment;
import com.example.gzysmartcity.viewmodel.HospViewModel;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class HospFragment extends BaseFragment<FragmentHospBinding> {
    private List<String> hospSpList;
    private List<String> keSHiSpList;
    private List<String> menZhenSpList;
    private ArrayAdapter<String> hospAdapter;
    private ArrayAdapter<String> keShiAdapter;
    private ArrayAdapter<String> menZhenAdapter;
    private String hospital, department, outpatient;
    private boolean firstHospital = true;
    private boolean firstDepartment = true;

    @Override
    protected FragmentHospBinding viewBinding() {
        return FragmentHospBinding.inflate(getLayoutInflater(), baseBinding.getRoot(), true);
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        HospViewModel hospViewModel = new ViewModelProvider(this).get(HospViewModel.class);
        b.rvHosp.setLayoutManager(new LinearLayoutManager(getContext()));
        b.rvHosp.setOverScrollMode(View.OVER_SCROLL_NEVER);


        keSHiSpList = new ArrayList<>();
        keSHiSpList.add("请选择科室");
        keShiAdapter = new ArrayAdapter<>(getContext(), R.layout.item_spanner, keSHiSpList);

        menZhenSpList = new ArrayList<>();
        menZhenSpList.add("请选择门诊");
        menZhenAdapter = new ArrayAdapter<>(getContext(), R.layout.item_spanner, menZhenSpList);


        hospViewModel.getSCHospitalData();
        hospViewModel.gethospLiveData().observe(getViewLifecycleOwner(), state -> {
            switch (state) {
                case SUCCESS:
                    //当未选择医院就点击科室时，提示用户应先选择医院，门诊亦如此。
                    List<HospData> hospDataList = new ArrayList<>(hospViewModel.getHospDataList());
                    HospRvAdapter adapter = new HospRvAdapter(R.layout.rv_hosp, hospDataList);
                    adapter.setNewInstance(hospDataList);
                    b.rvHosp.setAdapter(adapter);
                    //
                    hospSpList = new ArrayList<>();
                    hospSpList.add("请选择医院");
                    hospSpList.addAll(hospViewModel.getHospspList());
                    hospAdapter = new ArrayAdapter<>(getContext(), R.layout.item_spanner, hospSpList);
                    b.sphoYiyuan.setAdapter(hospAdapter);
                    b.sphoKeshi.setAdapter(keShiAdapter);
                    b.sphoMengzhen.setAdapter(menZhenAdapter);
                    b.sphoYiyuan.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                        @Override
                        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                            keSHiSpList.clear();
                            keSHiSpList.add("请选择科室");
                            if (position > 0) {
                                hospViewModel.getKsspList(keSHiSpList, hospSpList.get(position));
                                b.sphoKeshi.setSelection(0);
                                hospital = hospSpList.get(position);
                                firstHospital = false;
                            } else {
                                firstHospital = true;
                            }
                            keShiAdapter.notifyDataSetChanged();
                        }

                        @Override
                        public void onNothingSelected(AdapterView<?> parent) {
                        }
                    });
                    b.sphoKeshi.setOnTouchListener((v, event) -> {
                        if (firstHospital)
                            ToastUtils.showShort("请先选择医院");
                        return false;
                    });
                    b.sphoKeshi.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                        @Override
                        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                            Log.i("TAG", "onItemSelected: ");
                            department = null;
                            menZhenSpList.clear();
                            menZhenSpList.add("请选择门诊");
                            if (position != 0) {
                                hospViewModel.getMzSpLists(menZhenSpList, hospital, keSHiSpList.get(position));
                                b.sphoMengzhen.setSelection(0);
                                department = keSHiSpList.get(position);
                                firstDepartment = false;
                            } else {
                                firstDepartment = true;
                            }

                            menZhenAdapter.notifyDataSetChanged();
                        }

                        @Override
                        public void onNothingSelected(AdapterView<?> parent) {
                        }
                    });
                    b.sphoMengzhen.setOnTouchListener((View v, MotionEvent event) -> {
                        if (firstDepartment)
                            ToastUtils.showShort("请先选择科室");
                        return false;
                    });
                    b.sphoMengzhen.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                        @Override
                        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                            outpatient = null;
                            if (position != 0) {
                                outpatient = menZhenSpList.get(position);
                            }
                        }

                        @Override
                        public void onNothingSelected(AdapterView<?> parent) {
                        }
                    });
                    b.btnHospSearch.setOnClickListener(v -> {
                        if (hospital != null && department != null && outpatient != null) {
                            hospDataList.clear();
                            hospDataList.addAll(hospViewModel.search(hospital, department, outpatient));
                            adapter.notifyDataSetChanged();
                        } else {
                            ToastUtils.showShort("请先进行完整的选择再进行查询");
                        }
                    });
                    adapter.addChildClickViewIds(R.id.tv_hosp_chaozuo);
                    adapter.setOnItemChildClickListener((adapter1, view1, position) -> {
                        if (view1.getId() == R.id.tv_hosp_chaozuo) {
                            Bundle bundle = new Bundle();
                            bundle.putInt("mzId", hospDataList.get(position).getHospId());
                            jump(DoctorFragment.class, bundle, null);
                        }
                    });
                    break;
                case FAIL:
                    break;
            }
        });

    }

    @Override
    protected String setTitle() {
        return "预约挂号";
    }

    @Override
    protected boolean showBack() {
        return true;
    }

    @Override
    protected Boolean showBottom() {
        return null;
    }
}
